<?php
/**
 * Created by PhpStorm.
 * User: yanyan
 * Date: 2017/11/23
 * Time: 16:31
 */

namespace home\Controller;
use Think\Controller;

class MaterialController extends ApibaseController {
    /*
     * 耗材报备统计
     * @date: 2017-11-23下午5:56:13
     * @editor: zyy
     */
    public function reported(){
        /*参数初始化*/
        $page    = I('get.page', 1);
        $perpage = 10;
        $url     = [];
        $list    = M('MaterialReported');
        if (I('get.group_date')) {
            $list             = $list->where(['group_date' => I('get.group_date')]);
            $url['group_date'] = I('get.group_date');
        }
        if (I('get.group_place')) {
            $list             = $list->where(['group_place' => I('get.group_place')]);
            $url['group_place'] = I('get.group_place');
        }
        if (I('get.reporter')) {
            $list           = $list->where(['reporter' => I('get.reporter')]);
            $url['reporter'] = I('get.reporter');
        }
        $stime = I('get.start_date');
        $etime = I('get.end_date');
        if ($stime && !$etime) {
            $list   = $list->where(['add_at' => ['gt', strtotime($stime)]]);
            $url['stime'] = $stime;
        } elseif (!$stime && $etime) {
            $list   = $list->where(['add_at' => ['lt', strtotime($etime)]]);
            $url['etime'] = $etime;
        } elseif ($stime && $etime) {
            $list   = $list->where(['add_at' => ['between', [strtotime($stime), strtotime($etime)]]]);
            $url['stime'] = $stime;
            $url['etime'] = $etime;
        }
        $copyObject = clone $list;
        $report = M('MaterialReported')->where(['delete_at'=>0])->order('id desc')->page($page, $perpage)->select();
        $this->assign('reportData',$report);
        $this->assign('page', setPage($copyObject->count(), $perpage, $page, '?' . http_build_query($url)));
        $this->display('material/reported');
    }

    /*
     * 添加耗材报备
     * @date: 2017-11-24下午5:56:13
     * @editor: zyy
     */
    public function add_reported(){

        if(I('post.dosubmit')){
            if(I('post.id')){
                $effectId = M('MaterialReported')->where(['id'=>I('post.id')])->setField([
                    'group_class' => I('post.group_class'),
                    'group_place' => I('post.group_place'),
                    'group_doctor' => I('post.group_doctor'),
                    'group_date' => I('post.group_date'),
                    'report_content' => I('post.report_content'),
                    'reporter' => I('post.reporter'),
                    'operator' => getLoginUname(),
                    'update_at' => time()
                ]);
                if($effectId){
                    $this->showtips('耗材报备信息更新成功');
                }else{
                    $this->showtips('操作失败');
                }
            }
            $data = array(
                'group_class' => I('post.group_class'),
                'group_place' => I('post.group_place'),
                'group_doctor' => I('post.group_doctor'),
                'group_date' => I('post.group_date'),
                'report_content' => I('post.report_content'),
                'reporter' => I('post.reporter'),
                'operator' => getLoginUname(),
                'add_at' => time(),
                'update_at' => time()
            );
            if(I('post.group_date') && I('post.group_place') && I('post.report_content') && I('post.reporter')){
                $add_id = M('MaterialReported')->add($data);
                if($add_id){
                    $this->showtips('耗材报备信息添加成功');
                }else{
                    $this->showtips('操作失败');
                }
            }else{
                $this->showtips('报备信息参数缺失');
            }

        }
        if(I('get.id')){
            //报备信息详情
            $report = M('MaterialReported')->where(['id'=>I('get.id')])->find();
            $this->assign('reportData',$report);
        }
        $this->display('material/add_reported');
    }
    /*
     * 更新报备内容
     * @date: 2017-11-24下午5:56:13
     * @editor: zyy
     */
    public function edit_content(){
        if(I('post.dosubmit')){
            if(I('post.id')){
                $effectId = M('MaterialReported')->where(['id'=>I('post.id')])->setField([
                    'report_content' => I('post.report_content'),
                    'operator' => getLoginUname(),
                    'update_at' => time()
                ]);
                if($effectId){
                    $this->showtips('耗材报备内容更新成功');
                }else{
                    $this->showtips('操作失败');
                }
            }
        }
        if(I('get.id')){
            //报备信息详情
            $report = M('MaterialReported')->where(['id'=>I('get.id')])->find();
            $this->assign('reportData',$report);
        }
        $this->display('material/edit_content');
    }

    /*
     * 管理员根据报备内容添加备注
     * @date: 2017-11-27下午5:56:13
     * @editor: zyy
     */
    public function add_remark(){
        if(I('post.dosubmit') && I('post.remark')) {
            $effectId = M('MaterialReported')->where(['id' => I('post.id')])->setField([
                'remark' => I('post.remark'),
                'operator' => getLoginUname(),
                'update_at' => time()
            ]);
            if ($effectId) {
                $this->showtips('备注成功');
            } else {
                $this->showtips('操作失败');
            }
        }
        if(I('get.id')){
            //报备信息详情
            $report = M('MaterialReported')->where(['id'=>I('get.id')])->find();
            $this->assign('reportData',$report);
        }
        $this->display('material/add_remark');
    }

    public function delReported(){
        if (I('get.id')) {
            $del = M('MaterialReported')->where(['id' => I('get.id')])->delete();
            if ($del) {
                $this->showtips('删除成功');
            } else {
                $this->showtips('删除失败');
            }
        } else {
            $this->showtips('参数有误');
        }
    }
}